package com.nulldev.util.networking.npacket4.extensions.protocol;

import static com.nulldev.util.networking.npacket4.util.nPacketUtil.__readInt;

import java.io.IOException;
import java.io.InputStream;

import com.nulldev.util.VariableAPI.ByteUtil;
import com.nulldev.util.networking.npacket4.extensions.PacketExtension;
import com.nulldev.util.networking.npacket4.extensions.PacketExtensions.PacketExtensionID;

public class PacketPriorityExtension implements PacketExtension {

	private static final long serialVersionUID = 6573339801050777178L;

	public static enum PacketPriority {
		LOWEST(-2),
		LOW(-1),
		DEFAULT(0),
		MEDIUM(1),
		HIGH(2),
		VERY_HIGH(3),
		URGENT(4)

		;

		public final int id;

		private PacketPriority(final int id) {
			this.id = id;
		}

		public int getID() {
			return this.id;
		}

		public static PacketPriority get(final int id) {
			for (final PacketPriority packet : values()) {
				if (packet.id == id)
					return packet;
			}
			return DEFAULT;
		}
	}

	private PacketPriority priority = PacketPriority.DEFAULT;

	public PacketPriorityExtension() {
	}

	public PacketPriorityExtension(final PacketPriority priority) {
		this.priority = priority;
	}

	@Override
	public int extensionID() {
		return PacketExtensionID.EXT_PACKET_PRIORITY;
	}

	@Override
	public byte[] extensionData() {
		return ByteUtil.create();
	}

	@Override
	public byte[] processData(final byte[] data) {
		return data;
	}

	@Override
	public PacketExtension readExtensionData(final InputStream dis) throws IOException {
		this.priority = PacketPriority.get(__readInt(dis));
		return this;
	}

	public PacketPriority getPriority() {
		return this.priority;
	}

}